package cn.itcast.controller.cargo;

import cn.itcast.controller.BaseContorller;
import cn.itcast.domain.cargo.*;
import cn.itcast.service.cargo.ContractProductService;
import cn.itcast.service.cargo.ContractService;
import cn.itcast.service.cargo.ExtCproductService;
import cn.itcast.service.cargo.FactoryService;
import cn.itcast.utils.FileUploadUtil;
import com.alibaba.dubbo.config.annotation.Reference;
import com.github.pagehelper.PageInfo;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import java.util.Date;
import java.util.List;
import java.util.UUID;

@Controller
@RequestMapping("/cargo/extCproduct")
public class ExtCproductController extends BaseContorller {

    @Reference
    private FactoryService factoryService;
    @Reference
    private ExtCproductService extCproductService;
    @Reference
    private ContractService contractService;
    @Reference
    private ContractProductService contractProductService;

    @RequestMapping("/list")
    public String findAll(String contractId,String contractProductId, @RequestParam(value = "page",defaultValue = "1") Integer pageNum, @RequestParam(defaultValue = "5") Integer pageSize){
        //生产厂家
        //1.用来查询生产货物的工厂
        FactoryExample factoryExample = new FactoryExample();
        factoryExample.createCriteria().andCtypeEqualTo("附件");
        List<Factory> factoryList = factoryService.findAll(factoryExample);
        request.setAttribute("factoryList",factoryList);
        //2,查询这个购销合同货物下的附件
        ExtCproductExample example = new ExtCproductExample();
        example.createCriteria().andContractProductIdEqualTo(contractProductId);
        PageInfo pageInfo = extCproductService.findAll(example,pageNum,pageSize);
        request.setAttribute("page",pageInfo);

        request.setAttribute("contractId",contractId);
        request.setAttribute("contractProductId",contractProductId);
        return "cargo/extc/extc-list";
    }



    @Autowired
    private FileUploadUtil fileUploadUtil;


    @RequestMapping(value = "/edit",name = "保存货物下附件信息")
    public String edit(ExtCproduct extCproduct, MultipartFile productPhoto){

        try {
            String picPath = fileUploadUtil.upload(productPhoto);
            extCproduct.setProductImage(picPath);
        } catch (Exception e) {
            System.out.println("附件图片上传失败....");
            e.printStackTrace();
        }

        if (StringUtils.isEmpty(extCproduct.getId())){//id为空
            extCproduct.setId(UUID.randomUUID().toString());
            extCproduct.setCompanyId(getCompanyId());
            extCproduct.setCompanyName(getCompanyName());
            extCproduct.setCreateTime(new Date());
            extCproductService.save(extCproduct);

        }else {//id不为空
            extCproductService.update(extCproduct);
        }
        return "redirect:/cargo/extCproduct/list.do?contractId="+extCproduct.getContractId()+"&contractProductId="+extCproduct.getContractProductId();

    }



    @RequestMapping(value = "/toUpdate",name = "进入合同下货物修改页面")
    public String update(String id,String contractId,String contractProductId){
        //根据id查找附件
        ExtCproduct extCproduct = extCproductService.findById(id);
        request.setAttribute("extCproduct",extCproduct);
        //查询生产货物的厂家
        FactoryExample factoryExample = new FactoryExample();
        factoryExample.createCriteria().andCtypeEqualTo("附件");
        List<Factory> factoryList = factoryService.findAll(factoryExample);
        request.setAttribute("factoryList",factoryList);
        request.setAttribute("contractId",extCproduct.getContractId());
        request.setAttribute("contractProductId",extCproduct.getContractProductId());


        return "cargo/extc/extc-update";
    }

    @RequestMapping(value = "/delete",name = "删除购销合同货物下附件")
    public String delete(String id,String contractId,String contractProductId){
        extCproductService.delete(id); //删除货物
        return "redirect:/cargo/extCproduct/list.do?contractId="+contractId+"&contractProductId="+contractProductId;
    }

}
